package com.rustic.server.utils;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.hutool.crypto.SecureUtil;
import cn.xy.commons.exception.JzRuntimeException;
import com.rustic.server.bean.WeixinConfig;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenOAuth2ServiceImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *  @author xy
 */
@Slf4j
public class WxConfiguration {

    private static WxMaService wxMaService;

    private static WxOAuth2Service wxOAuth2Service;

    private WxConfiguration() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * 获取微信小程序相关实现
     *
     * @param weixinConfig 微信配置
     * @return WxMaService
     */
    public static WxMaService getMaService(WeixinConfig weixinConfig) {
        if (wxMaService == null) {
            // 小程序配置
            WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
            config.setAppid(weixinConfig.getAppId());
            config.setSecret(weixinConfig.getAppSecret());
            config.setMsgDataFormat("JSON");

            wxMaService = new WxMaServiceImpl();
            wxMaService.setWxMaConfig(config);
        }

        return wxMaService;
    }

    /**
     * 获取公众号相关实现
     *
     * @param weixinConfig 微信配置
     * @return WxOAuth2Service
     */
    public static WxOAuth2Service getOAuth2Service(WeixinConfig weixinConfig) {
        if (wxOAuth2Service == null) {
            WxOpenOAuth2ServiceImpl service = new WxOpenOAuth2ServiceImpl(weixinConfig.getAppId(), weixinConfig.getAppSecret());
            service.setWxOpenConfigStorage(new WxOpenInMemoryConfigStorage());
            wxOAuth2Service = service;
        }
        return wxOAuth2Service;
    }

    /**
     * 微信手机号解析
     *
     * @param wxService wxService
     * @param code      code
     * @return WxMaPhoneNumberInfo
     */
    public static WxMaPhoneNumberInfo getPhoneNoInfo(WxMaService wxService, String code) {
        try {
            return wxService.getUserService().getPhoneNoInfo(code);
        } catch (WxErrorException e) {
            throw new JzRuntimeException("微信手机号解析失败");
        }
    }

    /**
     * 服务器验签
     *
     * @param signature signature
     * @param timestamp timestamp
     * @param nonce     nonce
     * @param token     token
     * @return 验签结果
     */
    public static boolean checkSignature(String signature, String timestamp, String nonce, String token) {
        List<String> dataList = new ArrayList<>();
        dataList.add(token);
        dataList.add(timestamp);
        dataList.add(nonce);
        Collections.sort(dataList);
        String sha1 = SecureUtil.sha1(dataList.get(0) + dataList.get(1) + dataList.get(2));
        return sha1.equals(signature);
    }
}
